﻿using FastMvc.Models;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;

namespace FastMvc
{
    public class HttpHelper
    {
        public static async Task<BaseResult> GetAsync(string url)
        {
            return await Task.FromResult(Get(url));
        }

        public static async Task<BaseResult<T>> GetAsync<T>(string url)
        {
            return await Task.FromResult(Get<T>(url));
        }

        public static BaseResult Get(string url)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.GET;
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult>(re.ResHtml);
        }

        public static BaseResult<T> Get<T>(string url)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.GET;
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult<T>>(re.ResHtml);
        }

        public static async Task<BaseResult> PostAsync(string url, object Data)
        {
            return await Task.FromResult(Post(url, Data));
        }
        public static async Task<BaseResult<T>> PostAsync<T>(string url, object Data)
        {
            return await Task.FromResult(Post<T>(url, Data));
        }

        public static BaseResult Post(string url, object Data)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.POST;
            request.RequestBodyString = JsonConvert.SerializeObject(Data);
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult>(re.ResHtml);
        }
        public static BaseResult<T> Post<T>(string url, object Data)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.POST;
            request.RequestBodyString = JsonConvert.SerializeObject(Data);
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult<T>>(re.ResHtml);
        }

        public static async Task<BaseResult> PutAsync(string url, object Data)
        {
            return await Task.FromResult(Put(url, Data));
        }

        public static async Task<BaseResult<T>> PutAsync<T>(string url, object Data)
        {
            return await Task.FromResult(Put<T>(url, Data));
        }

        public static BaseResult Put(string url, object Data)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.PUT;
            request.RequestBodyString = JsonConvert.SerializeObject(Data);
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult>(re.ResHtml);
        }
        public static BaseResult<T> Put<T>(string url, object Data)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.PUT;
            request.RequestBodyString = JsonConvert.SerializeObject(Data);
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult<T>>(re.ResHtml);
        }

        public static async Task<BaseResult> DeleteAsync(string url)
        {
            return await Task.FromResult(Delete(url));
        }

        public static async Task<BaseResult<T>> DeleteAsync<T>(string url)
        {
            return await Task.FromResult(Delete<T>(url));
        }

        public static BaseResult Delete(string url)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.DELETE;
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult>(re.ResHtml);
        }

        public static BaseResult<T> Delete<T>(string url)
        {
            HttpRequest request = new HttpRequest();
            request.Address = url;
            request.Method = HttpMethod.DELETE;
            var re = request.Proc();
            return JsonConvert.DeserializeObject<BaseResult<T>>(re.ResHtml);
        }
    }
}
